package network

import (
	"fmt"
	"vpn/guard/utils"

	"github.com/songgao/water"
)

func NewTun(link string, mtu int) (*water.Interface, error) {
	config := water.Config{
		DeviceType: water.TUN,
	}

	ifce, err := water.New(config)
	if err != nil {
		return nil, fmt.Errorf("failed to create TUN interface: %v", err)
	}
	utils.ExecCmd(fmt.Sprintf("ip addr add %s dev %s", link, ifce.Name()))
	utils.ExecCmd(fmt.Sprintf("ip link set dev %s up", ifce.Name()))
	utils.ExecCmd(fmt.Sprintf("ip link set dev %s mtu %d", ifce.Name(), mtu))
	return ifce, nil
}
